%% computepressureheartrate
%
% Calculates heart rate from pressure waveform data
%
%% Synatax
%
% [heartRate, pressureWaveformData] = computepressureheartrate(filePathPressure, SAMPLING_FREQUENCY, instructionText);
%
% [60, pressureWaveformData] = computepressureheartrate('C:\Path\To\waveform.xls', 128, instructionText);
%
% [77, pressureWaveformData] = computepressureheartrate('C:/Path/To/waveformt.txt', 128, instructionText);
%
%% Description
%
% This function attempts to calculate the heart rate during measurement from 
% imported pressure waveform data and user defined sampling frequency. 
%
%% Arguments
%
%* filePathPressure                - char: Path to the waveform file of
%                                          interest 
%
%* SAMPLING_FREQUENCY              - double: The frequency (Hz) at which
%                                            the pressure waveform data was 
%                                            sampled
%
%* instructionText                 - matlab.ui.control.UIControl (Object): 
%                                    Text box parameters for the GUI
%
%% Returns
%
%* heartRate                       - double: The calculated heart rate
%                                            from pressure waveform data
%
%* pressureWaveformData            - double: The array of values imported
%                                            from the pressure waveform
%                                            file
%
%% Function Side Effects
%
%* The GUI instruction box text will change
%
%* A warning will be printed if the calculated pressure heart rate value is 
%  not numeric (byproduct of using validateHeartRate for validtion)
%
%* A warning will be printed if the calculated pressure heart rate value is
%  empty (byproduct of using validateHeartRate for validtion)
%
%* A warning will be printed if the calculated heart rate is not between 20 
%  and 200
%
%% Exceptions
%
%* An exception will be thrown if filePathPressure is not a valid file path
%
%* An exception will be thrown if filePathPressure does not point to a file
%  with an .xls or .txt extension
%
%* An exception will be thrown if the imported pressure waveform file is
%  empty
%
%% See Also
%
% impedanceOnlyFunc
% 


function [heartRate, pressureWaveformData] = computepressureheartrate(filePathPressure, SAMPLING_FREQUENCY, instructionText)

set(instructionText, 'string','Unable to ascertain heart rate from Redcap. Attempting to determine HR directly from file: ');

pause(2);

% Validates path points to a file
if exist(filePathPressure, 'file') ~= 2
    error('computepressureheartrate:invalidDirectoryPath',...
          'Error: \nunable to find file matching fully qualified path %s', filePathPressure);
end

% Validates path points to a valid file type
[filePathPressurePathStrfile,filePathPressureFileName,filePathPressureExt] = fileparts(filePathPressure);

assert(strcmp('.xls', filePathPressureExt) || strcmp('.txt', filePathPressureExt), ...
       'computepressureheartrate:invalidFile', ...
       'The file at path %s is not a valid file type (has extension %s)', filePathPressure, filePathPressureExt);        


pressureWaveformData = csvread(filePathPressure);

if isempty(pressureWaveformData)
    
    error('computepressureheartrate:emptyWaveformFile',...
          'Error: \nImported pressure waveform file is empty'); 
      
end

nSamples = (length(pressureWaveformData));

heartRate = ( 1 / ( nSamples / SAMPLING_FREQUENCY ) * 60 );
heartRate = round(heartRate, 0);

validationState = 0;
validationState = validateHeartRate(heartRate);

if validationState == 1
    
    set(instructionText, 'string','Pressure heart rate successfully calculated');
    
else 
    
    set(instructionText, 'string','Failed to calculate pressure heart rate');
    pause(2);
    
end

end

